<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/7/11
 * Time: 11:02
 */

namespace app\admin\controller;


use think\Db;

class ProjectController extends CommonController
{
    //项目类型
    private $project_type_name = [
        1 => '推荐项目',
        2 => '在售项目',
        3 => '即将开售',
    ];
    
    /**
     * 项目banner展示
     * @param $page  页数
     * @param $rows  每页显示的条数
     * @return string
     */
    public function projectBanner(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $res = Db::table('ew_projectbanner')->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_projectbanner')->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    
    /**
     * banner添加页面
     * @return mixed
     */
    public function addBanner(){
        return $this->fetch('project/addbanner');
    }
    
    /**
     * banner添加入库
     * @return string
     */
    public function insertBanner(){
        if($_FILES['fileImg']['name']==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "图片不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['banner_content'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "内容不能为空";
            return json_encode($returnData);
        }
        //1
        $rand = date('YmdHis').rand(100000,999999);
        $exe = explode('.',$_FILES['fileImg']['name']);
        $filePath = $_FILES['fileImg']['tmp_name'];
        $img_name = $rand.'.'.$exe[1];
        $_filePath = "static/image/projectbanner/".$img_name;
        move_uploaded_file($filePath,$_filePath);
        $data['banner_url'] = $_filePath;
        $data['banner_content'] = $_POST['banner_content'];
        $data['creat_time'] = date('Y-m-d H:i:s');
        $res = Db::table('ew_projectbanner')->insert($data);
        if($res){
            return json_encode(array("status" => 1, "msg" => "添加成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "添加失败！"));
        }
    }
    
    /**
     * banner修改页面
     * @return mixed
     */
    public function updateBanner(){
        $banner_id = $_POST['banner_id'];
        $res = Db::table('ew_projectbanner')->where('banner_id',$banner_id)->find();
        $this->assign('res',$res);
        return $this->fetch('project/updatebanner');
    }
    
    /**
     * banner修改入库
     * @return string
     */
    public function updateB(){
        if(!empty($_FILES['fileImg']['type'])){
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['fileImg']['name']);
            $filePath = $_FILES['fileImg']['tmp_name'];
            $img_name = $rand.'.'.$exe[1];
            $_filePath = 'static/image/projectbanner/'.$img_name;
            move_uploaded_file($filePath,$_filePath);
            $data['banner_url'] = $_filePath;
        }
        if(trim($_POST['banner_content'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "内容不能为空";
            return json_encode($returnData);
        }
        $data['banner_content'] = $_POST['banner_content'];
        $data['creat_time'] = date('Y-m-d H:i:s');
        $res = Db::table('ew_projectbanner')->where('banner_id',$_POST['banner_id'])->update($data);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败";
            return json_encode($returnData);
        }
    }
    
    /***
     * banner删除
     * @return string
     *
     */
    public function deleteBanner(){
        $banner_id = $_POST['banner_id'];
        if ($banner_id != '') {
            $where['banner_id'] = $banner_id;
            $res = Db::table('ew_projectbanner')->where($where)->delete();
            if ($res) {
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            } else {
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }
    }
    
    /**
     * 项目房屋展示
     * @param $page  页数
     * @param $rows  每页显示的条数
     * @return string
     */
    public function projectHouse(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_projecthouse')->limit($rows * ($page - 1), $rows)->where($where)->select();
        foreach($res as $key=>&$val){
            $val['h_project_type'] = $this->project_type_name[$val['h_project_type']];
        }
        $total = Db::table('ew_projecthouse')->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    
    /**
     * 房屋添加页面
     * @return mixed
     */
    public function addHouse(){
        $project_type = Db::name('project')->select();
        $this->assign([
            'project_type' => $project_type
        ]);
        return $this->fetch('project/addhouse');
    }
    
    /**
     * 房屋添加入库
     * @return string
     */
    public function insertHouse(){
        if(trim($_POST['h_name'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "房屋名称不能为空";
            return json_encode($returnData);
        }
        if($_FILES['fileImg']['name']==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "图片不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_price'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "房屋总价不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_yield_tras'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "收益周期不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_auth_equity'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "会员权益不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_prospective'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "预期收益不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_rental'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "租金收益不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_asset'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "资产收益不能为空";
            return json_encode($returnData);
        }
        $rand = date('YmdHis').rand(100000,999999);
        $exe = explode('.',$_FILES['fileImg']['name']);
        $filePath = $_FILES['fileImg']['tmp_name'];
        $img_name = $rand.'.'.$exe[1];
        $_filePath = "static/image/projecthouse/".$img_name;
        move_uploaded_file($filePath,$_filePath);
        $data['h_image'] = $_filePath;
        $data['h_name'] = $_POST['h_name'];
        $data['h_price'] = $_POST['h_price'];
        $data['h_yield_tras'] = $_POST['h_yield_tras'];
        $data['h_auth_equity'] = $_POST['h_auth_equity'];
        $data['h_prospective'] = $_POST['h_prospective'];
        $data['h_rental'] = $_POST['h_rental'];
        $data['h_asset'] = $_POST['h_asset'];
        $data['h_project_type'] = $_POST['h_project_type'];
        $res = Db::table('ew_projecthouse')->insert($data);
        if($res){
            return json_encode(array("status" => 1, "msg" => "添加成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "添加失败！"));
        }
    }
    
    /**
     * house修改页面
     * @return mixed
     */
    public function updateHouse(){
        $h_id = $_POST['h_id'];
        $res = Db::table('ew_projecthouse')->where('h_id',$h_id)->find();
        $project_type = Db::name('project')->select();
        $this->assign([
            'project_type' => $project_type,
            'res' => $res
        ]);
        return $this->fetch('project/updatehouse');
    }
    
    /**
     * house修改入库
     * @return string
     */
    public function updateH(){
        if(!empty($_FILES['fileImg']['type'])){
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['fileImg']['name']);
            $filePath = $_FILES['fileImg']['tmp_name'];
            $img_name = $rand.'.'.$exe[1];
            $_filePath = 'static/image/projecthouse/'.$img_name;
            move_uploaded_file($filePath,$_filePath);
            $data['h_image'] = $_filePath;
        }
        if(trim($_POST['h_name'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "房屋名称不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_price'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "房屋总价不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_yield_tras'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "收益周期不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_auth_equity'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "会员权益不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_prospective'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "预期收益不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_rental'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "租金收益不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['h_asset'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "资产收益不能为空";
            return json_encode($returnData);
        }
        $data['h_name'] = $_POST['h_name'];
        $data['h_price'] = $_POST['h_price'];
        $data['h_yield_tras'] = $_POST['h_yield_tras'];
        $data['h_auth_equity'] = $_POST['h_auth_equity'];
        $data['h_prospective'] = $_POST['h_prospective'];
        $data['h_rental'] = $_POST['h_rental'];
        $data['h_asset'] = $_POST['h_asset'];
        $data['h_project_type'] = $_POST['h_project_type'];
        $res = Db::table('ew_projecthouse')->where('h_id',$_POST['h_id'])->update($data);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败";
            return json_encode($returnData);
        }
    }
    
    /***
     * @return string
     * 房屋删除
     */
    public function deleteHouse(){
        $h_id = $_POST['h_id'];
        if ($h_id != '') {
            $where['h_id'] = $h_id;
            $res = Db::table('ew_projecthouse')->where($where)->delete();
            if ($res) {
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            } else {
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }
    }
}